package com.tiny.mirror.service.product.domain.platform.service;

import com.tiny.mirror.basic.db.domain.Aggregate;
import com.tiny.mirror.basic.db.domain.AggregateFactory;
import com.tiny.mirror.service.product.domain.platform.Platform;
import com.tiny.mirror.service.product.domain.platform.repository.PlatformRepository;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/25
 */
@Service
public class PlatformDomainService {

  @Resource
  private PlatformRepository platformRepository;

  public Long create(Platform platform) {
    Aggregate<Platform> platformAggregate = AggregateFactory.createEmptyAggregate();
    platformAggregate.superAdd(platform);
    return platformRepository.save(platformAggregate).getRoot().getId();
  }

  public void update(Long id, Platform platform) {
    Aggregate<Platform> platformAggregate = platformRepository.findById(id);
    platformAggregate.getRoot().edit(platform);
    platformRepository.save(platformAggregate);
  }

  public void delete(Long id) {
    Aggregate<Platform> platformAggregate = platformRepository.findById(id);
    platformAggregate.delete();
    platformRepository.save(platformAggregate);
    platformRepository.savePlatformAbility(id, null);
  }
}
